<template>
	<view>
		<view class="content">
			<view class="row">
				<view class="nominal">
					收件人
				</view>
				<view class="input">
					<input placeholder="请输入收件人姓名" type="text" v-model="name" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					电话号码
				</view>
				<view class="input">
					<input placeholder="请输入收件人电话号码" type="text" v-model="phone" />
				</view>
			</view>
			<view class="row">
				
				<view class="nominal">
					所在地区
				</view>
				<view style="align-items: center;" @click="addressPickerClick">
					{{provincesAndMunicipalitiesName ? provincesAndMunicipalitiesName : "请选择省市区"}}
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					详细地址
				</view>
				<view class="input">
					<textarea v-model="detailed" auto-height="true" placeholder="输入详细地址"></textarea>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					设置默认地址
				</view>
				<view class="input switch">
					<switch color="#f06c7a" :checked="isDefault" @change=isDefaultChange />
				</view>
			</view>
			<view class="row" v-if="editType=='edit'" @tap="del">
				<view class="del">
					删除收货地址
				</view>
			</view>
		</view>
		<view class="save" @tap="save">
			<view class="btn">
				保存地址
			</view>
		</view>
		<area-picker ref="areaPicker" @conceal="conceal" @closePicker="closePicker" />
	</view>
</template>

<script>
	
	import areaPicker from '@/components/areaPicker/areaPicker';
	export default {
		components: {
			areaPicker
		},
		data() {
			return {
				provincesAndMunicipalitiesName: "",
				editType:'edit',
				id:'',
				name:'',
				phone:'',
				detailed:'',
				isDefault:false,
				cityPickerValue: [0, 0, 1],
				themeColor: '#007AFF',
				region:{label:"请点击选择地址",value:[],cityCode:""},
				province_id: "",
				city_id: "",
				area_id: "",
			};
		},
		methods: {
			// 选择的省市区
			addressPickerClick() {
				this.$refs.areaPicker.showPickerModal();
			},
			conceal(param) {
				this.province_id = param.province.id;
				this.city_id = param.city.id;
				this.area_id = param.area.id;
				this.provincesAndMunicipalitiesName = param.province.name + "/" + param.city.name + "/" + param.area.name;
			},
			isDefaultChange(e){
				this.isDefault = e.detail.value;
			},
			del(){
				var that=this;
				uni.showModal({
					title: '删除提示',
					content: '你将删除这个收货地址',
					success: (res)=>{
						if (res.confirm) {
							that.$http.post('api/me/delAddress', {
								'id':this.id
							}).then((res) => {
						if(res.code == 200){
							uni.showToast({
								title:res.msg,
								icon:'none',
								success() {
									setTimeout(function(){
										uni.navigateBack({
											delta:1
										})
									},2000)
								}
							})
						}
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},
			save(){
				var that=this;
				let data={"name":this.name,"head":this.name.substr(0,1),"phone":this.phone,"address":{"region":this.region,"detailed":this.detailed},"isDefault":this.isDefault}
				
				console.log(data)
				if(this.editType=='edit'){
					data.id = this.id
				}
				if(!data.name){
					uni.showToast({title:'请输入收件人姓名',icon:'none'});
					return ;
				}
				if(!data.phone){
					uni.showToast({title:'请输入收件人电话号码',icon:'none'});
					return ;
				}
				if(!data.address.detailed){
					uni.showToast({title:'请输入收件人详细地址',icon:'none'});
					return ;
				}
				uni.showLoading({
					title:'正在提交'
				})
				
				that.$http.post('me/addEditAddress', {
					'phone':data.phone,
					'name':data.name,
					'detailed':data.address.detailed,
					'province_id':this.province_id,
					'city_id':this.city_id,
					'area_id':this.area_id,
					'is_default':this.isDefault,
					"id":this.id
				}).then((res) => {
					if(res.code==200){
						uni.showToast({
							title:res.msg,
							icon:'none',
							success() {
								setTimeout(function(){
									uni.navigateBack();
								},2000)
							}
						})
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
					 
				});
				
				
			},
			
			addressInfor(id) {
				let params = {
					id: id
				};
				this.$ajax("api/me/addressDetails", params, "POST", true).then(res => {
					this.id = res.data.id;
					this.name = res.data.name;
					this.phone = res.data.phone;
					this.province_id = res.data.province.id;
					this.city_id = res.data.city.id;
					this.area_id = res.data.area.id;
					this.provincesAndMunicipalitiesName = res.data.province.name + "/" + res.data.city.name + "/" +
						res.data.area.name;
					this.detailed = res.data.detailed;
					this.is_default = res.data.is_default;
				}).catch(err => {
					uni.showToast({
						title: err.msg,
						icon: "none"
					})
				})
			},
		},
		onLoad(e) {
			//获取传递过来的参数
			
			this.editType = e.type;
			if(e.type=='edit'){
				this.addressInfor(e.id)
				this.id=e.id
			}
			
		},

		onUnload() {

		}
	};
</script>
<style lang="scss">

.save{
		view{
			display: flex;
		}
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;
		.btn{
			box-shadow: 0upx 5upx 10upx rgba(0,0,0,0.4);
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			background-color: #f06c7a;
			color: #fff;
			justify-content: center;
			align-items: center;
			.icon{
				height: 80upx;
				color: #fff;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}
			font-size: 30upx;
		}
	}
	.content{
		display: flex;
		flex-wrap: wrap;
		view{
			display: flex;
		}
		.row{
			width: 94%;
			
			margin: 0 3%;
			border-top: solid 1upx #eee;
			.nominal{
				width: 30%;
				height: 120upx;
				font-weight: 200;
				font-size: 30upx;
				align-items: center;
			}
			.input{
				width: 70%;
				padding: 20upx 0;
				align-items: center;
				font-size: 30upx;
				&.switch{
					justify-content: flex-end;
				}
				.textarea{
					margin: 20upx 0;
					min-height: 120upx;
				}
			}
			.del{
				width: 100%;
				height: 100upx;
				justify-content: center;
				align-items: center;
				font-size: 36upx;
				color: #f06c7a;
				background-color: rgba(255,0,0,0.05);
				border-bottom: solid 1upx #eee;
			}
		}
	}
	
</style>
